package com.randi.framework.domain.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.randi.common.core.domain.dto.TreeNode;
import com.randi.common.core.domain.entity.SysMenu;
import com.randi.common.enums.MenuTypeEnum;
import com.randi.common.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * @author ziye
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel(value = "Router", description = "路由对象")
public class Router extends TreeNode {

    @ApiModelProperty(value = "路由名字", notes = "/**")
    private String name;

    @ApiModelProperty(value = "路由地址", notes = "/**")
    private String path;

    @ApiModelProperty(value = "重定向地址", notes = "/**")
    private String redirect;

    @ApiModelProperty(value = "组件地址", notes = "/**")
    private String component;

    @ApiModelProperty(value = "路由参数", notes = "param1, param2")
    private String query;

    @ApiModelProperty(value = "路由元信息")
    private RouterMeta meta;

    /**
     * 根菜单路径
     */
    private static final String ROUTER_CATALOG_COMPONENT = "Outlet";

    private static final String ROUTER_PATH_CONNECTOR = "/";

    public Router(SysMenu sysMenu) {
        this.id = sysMenu.getMenuId();
        this.parentId = sysMenu.getParentId();
        this.component = sysMenu.getComponent();
        this.path = sysMenu.getMenuPath();
        this.query = sysMenu.getQuery();
        this.meta = new RouterMeta(sysMenu);

        if (sysMenu.getMenuType() == MenuTypeEnum.CATALOG) {
            this.name = StringUtils
                    .substring(sysMenu.getMenuPath(), 1)
                    .replaceAll(ROUTER_PATH_CONNECTOR, "-");
        } else if (sysMenu.getMenuType() == MenuTypeEnum.MENU) {
            this.name = StringUtils.substring(sysMenu.getComponent(), 1)
                    .replace("/index", "")
                    .replaceAll(ROUTER_PATH_CONNECTOR, "-");
        } else {
            this.name = sysMenu.getMenuCode().replaceAll(":", "-");
        }

        if (StringUtils.isEmpty(component) && sysMenu.getMenuType() == MenuTypeEnum.CATALOG) {
            component = ROUTER_CATALOG_COMPONENT;
        }

    }

}
